/*
 * Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
 * 
 * Copyright 2009 by Sebasti�n Zaffarano
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 */
package org.sigedu.run;

import java.util.LinkedList;
import java.util.List;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.sigedu.ComponentsModule;
import org.sigedu.core.CoreModule;
import org.sigedu.security.SecurityModule;

/**
 * @author sebas
 */
public class RunJetty {
	private static final List<Class<?>> modules;

	static {
		/* por el tema refactorings */
		modules = new LinkedList<Class<?>>();
		modules.add(SecurityModule.class);
		modules.add(CoreModule.class);
		modules.add(ComponentsModule.class);
	}

	public static void main(final String[] args) throws Exception {

		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < modules.size(); i++) {
			builder.append(modules.get(i).getName());
			if (i < modules.size() - 1) {
				builder.append(", ");
			}
		}
		System.setProperty("tapestry.modules", builder.toString());

		Server server = new Server();

		Connector connector = new SelectChannelConnector();
		connector.setPort(Integer.getInteger("jetty.port", 8080));
		server.setConnectors(new Connector[] { connector });

		String jetty_home = System.getProperty("jetty.home", "./");

		WebAppContext webapp = new WebAppContext(jetty_home + "src/main/webapp", "/sigedu");

		// Remove slf4j from list of classes not exposed to webapp
		webapp.setServerClasses(new String[] { "-org.mortbay.jetty.plus.jaas.", "org.mortbay.jetty." });

		server.addHandler(webapp);

		server.start();

		Runtime.getRuntime().exec("firefox http://localhost:8080/sigedu/");

		server.join();

	}

}
